package com.chuanyang.driverh.mainorder.fragment;


import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainorder.DeliveryOrderDetailActivity;
import com.chuanyang.driverh.responseresult.DeliverOrderData;
import com.chuanyang.driverh.responseresult.DeliverOrderResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class DeliveryOrderFragment extends BaseListFragment {
    private ViewHolder holder;
    private HashMap params;
    private DeliveryOrderAdapter adapter;
    private DeliverOrderResult deliverOrderResult;
    private String waybillid;
    private int driverId;

    @Override
    public void initView(View view) {
        super.initView(view);
        waybillid = getActivity().getIntent().getStringExtra("data");
        driverId = getActivity().getIntent().getIntExtra("driverId",0);
        getData();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public BaseListAdapter createAdapter() {
        return adapter= new DeliveryOrderAdapter(getActivity());
    }

    @Override
    protected void onForceRefresh() {
        super.onForceRefresh();
        getData();
    }




    private void getData() {
        mCurrentPage=1;
        DialogUtil.showLoadingDialog(getActivity(),"清单...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId",driverId+"" );
        MyHttpUtil.getForZHJ(getActivity(),params, new MyCallback<DeliverOrderResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                springView.onFinishFreshAndLoad();
            }
            @Override
            public void onSuccess(DeliverOrderResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    setDataResult(result.getData());
                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });


    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        DeliverOrderData databean= (DeliverOrderData) adapter.getItem(position);
        Intent intent=new Intent(getActivity(),DeliveryOrderDetailActivity.class);
        intent.putExtra("data",databean);
        intent.putExtra("libraryId",databean.getWaybillId());
        intent.putExtra("waybillid",waybillid);
        startActivity(intent);
    }


    private  class  DeliveryOrderAdapter extends BaseListAdapter {
        public DeliveryOrderAdapter(Context context) {
            super(context);
        }

        @Override
        public View getZkView(int position, View convertView, ViewGroup parent) {
            if(convertView==null)
            {
                convertView = getInflater().inflate(R.layout.order_item_deliveryorder, parent, false);
                holder = new ViewHolder();
                holder.tv_delivery_mainnum= (TextView) convertView.findViewById(R.id.tv_delivery_mainnum);
                holder.tv_delivery_time= (TextView) convertView.findViewById(R.id.tv_delivery_time);
//               holder.tv_delivery_num= (TextView) convertView.findViewById(R.id.tv_delivery_num);
//               holder.tv_delivery_status= (TextView) convertView.findViewById(R.id.tv_delivery_status);
//               holder.tv_delivery_weight= (TextView) convertView.findViewById(R.id.tv_delivery_weight);
//               holder.tv_waybill_count= (TextView) convertView.findViewById(R.id.tv_waybill_count);
                convertView.setTag(holder);
            }
            else {
                holder= (ViewHolder) convertView.getTag();
            }
            DeliverOrderData bean= (DeliverOrderData) getItem(position);
            holder.tv_delivery_mainnum.setText(bean.getMainListingNumber());
            holder.tv_delivery_time.setText(bean.getDateEndSuffix());
            return convertView;
        }
    }
    class  ViewHolder{
        TextView tv_delivery_mainnum;
        TextView tv_delivery_time;

    }

    @Override
    protected boolean isPullToRefresh() {
        return false;
    }

    @Override
    protected boolean isLoadMore() {
        return false;
    }
}
